 package server.shop;


import com.alibaba.druid.pool.DruidPooledConnection;
import com.fasterxml.jackson.core.JsonProcessingException;
import constants.ItemConstants;
import database.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.MapleItemInformationProvider;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;















 public class MapleShopFactory {
/*  18 */   private static final Logger log = LogManager.getLogger(MapleShopFactory.class.getName());
/*  19 */   private static final Set<Integer> rechargeableItems = new LinkedHashSet<>();
/*  20 */   private static final Set<Integer> blockedItems = new LinkedHashSet<>();
/*  21 */   private static final MapleShopFactory instance = new MapleShopFactory();

   static {
/*  24 */     rechargeableItems.add(Integer.valueOf(2070000));
/*  25 */     rechargeableItems.add(Integer.valueOf(2070001));
/*  26 */     rechargeableItems.add(Integer.valueOf(2070002));
/*  27 */     rechargeableItems.add(Integer.valueOf(2070003));
/*  28 */     rechargeableItems.add(Integer.valueOf(2070004));
/*  29 */     rechargeableItems.add(Integer.valueOf(2070005));
/*  30 */     rechargeableItems.add(Integer.valueOf(2070006));
/*  31 */     rechargeableItems.add(Integer.valueOf(2070007));
/*  32 */     rechargeableItems.add(Integer.valueOf(2070008));
/*  33 */     rechargeableItems.add(Integer.valueOf(2070009));
/*  34 */     rechargeableItems.add(Integer.valueOf(2070010));
/*  35 */     rechargeableItems.add(Integer.valueOf(2070011));
/*  36 */     rechargeableItems.add(Integer.valueOf(2070012));
/*  37 */     rechargeableItems.add(Integer.valueOf(2070013));
/*  38 */     rechargeableItems.add(Integer.valueOf(2070015));
/*  39 */     rechargeableItems.add(Integer.valueOf(2070016));
/*  40 */     rechargeableItems.add(Integer.valueOf(2070019));
/*  41 */     rechargeableItems.add(Integer.valueOf(2070020));
/*  42 */     rechargeableItems.add(Integer.valueOf(2070021));
/*  43 */     rechargeableItems.add(Integer.valueOf(2070023));
/*  44 */     rechargeableItems.add(Integer.valueOf(2070024));
/*  45 */     rechargeableItems.add(Integer.valueOf(2330000));
/*  46 */     rechargeableItems.add(Integer.valueOf(2330001));
/*  47 */     rechargeableItems.add(Integer.valueOf(2330002));
/*  48 */     rechargeableItems.add(Integer.valueOf(2330003));
/*  49 */     rechargeableItems.add(Integer.valueOf(2330004));
/*  50 */     rechargeableItems.add(Integer.valueOf(2330005));
/*  51 */     rechargeableItems.add(Integer.valueOf(2330006));
/*  52 */     rechargeableItems.add(Integer.valueOf(2330007));
/*  53 */     rechargeableItems.add(Integer.valueOf(2330008));


/*  56 */     blockedItems.add(Integer.valueOf(4170023));
/*  57 */     blockedItems.add(Integer.valueOf(4170024));
/*  58 */     blockedItems.add(Integer.valueOf(4170025));
/*  59 */     blockedItems.add(Integer.valueOf(4170028));
/*  60 */     blockedItems.add(Integer.valueOf(4170029));
/*  61 */     blockedItems.add(Integer.valueOf(4170031));
/*  62 */     blockedItems.add(Integer.valueOf(4170032));
/*  63 */     blockedItems.add(Integer.valueOf(4170033));
   }

/*  66 */   private final Map<Integer, MapleShop> shops = new TreeMap<>();
/*  67 */   private final Map<Integer, Integer> shopIDs = new TreeMap<>();

   public static MapleShopFactory getInstance() {
/*  70 */     return instance;
   }

   public static void updateShop(MapleShop shop) {
/*  74 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  75 */       boolean addshop = true;
/*  76 */       try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM shops WHERE shopid = ?")) {
/*  77 */         ps.setInt(1, shop.getId());
/*  78 */         try (ResultSet rs = ps.executeQuery()) {
/*  79 */           if (rs.next() && 
/*  80 */             rs.getInt(1) > 0) {
/*  81 */             addshop = false;
           }
         }
       }


/*  87 */       if (addshop) {
/*  88 */         try (PreparedStatement ps = con.prepareStatement("INSERT INTO shops(shopid, npcid) VALUES (?, ?)")) {
/*  89 */           ps.setInt(1, shop.getId());
/*  90 */           ps.setInt(2, shop.getNpcId());
/*  91 */           ps.executeUpdate();
         }
       }

/*  95 */       try (PreparedStatement ps = con.prepareStatement("DELETE FROM shopitems WHERE shopid = ?")) {
/*  96 */         ps.setInt(1, shop.getId());
/*  97 */         ps.executeUpdate();
       }

/* 100 */       try (PreparedStatement ps = con.prepareStatement("INSERT INTO shopitems VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
/* 101 */         for (MapleShopItem item : shop.getItems()) {
/* 102 */           if (rechargeableItems.contains(Integer.valueOf(item.getItemId())) && item.getBuyable() == 1) {
             continue;
           }
/* 105 */           ps.setInt(1, shop.getId());
/* 106 */           ps.setInt(2, item.getItemId());
/* 107 */           ps.setInt(3, item.getPrice());
/* 108 */           ps.setInt(4, item.getPosition());
/* 109 */           ps.setInt(5, item.getReqItem());
/* 110 */           ps.setInt(6, item.getReqItemQ());
/* 111 */           ps.setInt(7, item.getPointtype());
/* 112 */           ps.setInt(8, item.getPeriod());
/* 113 */           ps.setInt(9, item.getState());
/* 114 */           ps.setInt(10, item.getCategory());
/* 115 */           ps.setInt(11, item.getMinLevel());
/* 116 */           ps.executeUpdate();
         }
       }
/* 119 */     } catch (SQLException ignored) {
/* 120 */       ignored.printStackTrace();
     }
   }

   public static void removeShop(int shopid) {
/* 125 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 126 */         PreparedStatement ps = con.prepareStatement("DELETE FROM shops WHERE shopid = ?")) {
/* 127 */       ps.setInt(1, shopid);
/* 128 */       ps.executeUpdate();
     }
/* 130 */     catch (SQLException ignored) {
/* 131 */       ignored.printStackTrace();
     }
   }


   public void setShopItemData(String shopid, int npcid, List<MapleShopItem> shopItems) throws JsonProcessingException {
/* 137 */     List<MapleShopItem> shopItemsin = new ArrayList<>();

/* 139 */     MapleShop mapleShop = new MapleShop(Integer.valueOf(shopid).intValue(), npcid);

/* 141 */     for (MapleShopItem shopItem : shopItems) {
/* 142 */       if (shopItem.isRechargeableItem()) {
         continue;
       }
/* 145 */       shopItemsin.add(shopItem);
/* 146 */       mapleShop.addItem(shopItem);
     }

/* 149 */     this.shops.put(Integer.valueOf(shopid), mapleShop);
/* 150 */     update(Integer.valueOf(shopid).intValue(), shopItemsin);
   }

   public void update(int shopid, List<MapleShopItem> shopItems) {
/* 154 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 155 */         PreparedStatement ps = con.prepareStatement("DELETE FROM shopitems WHERE shopid = ?")) {
/* 156 */       ps.setInt(1, shopid);
/* 157 */       ps.execute();
     }
/* 159 */     catch (SQLException sQLException) {}



/* 163 */     if (shopItems != null) {
/* 164 */       try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 165 */           PreparedStatement ps = con.prepareStatement("INSERT INTO shopItems VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
/* 166 */         shopItems.forEach(shopItems1 -> {
               try {
                 ps.setInt(1, shopid);
                 ps.setInt(2, shopItems1.getItemId());
                 ps.setInt(3, shopItems1.getPrice());
                 ps.setInt(4, shopItems1.getPosition());
                 ps.setInt(5, shopItems1.getReqItem());
                 ps.setInt(6, shopItems1.getReqItemQ());
                 ps.setInt(7, shopItems1.getPointtype());
                 ps.setInt(8, shopItems1.getPeriod());
                 ps.setInt(9, shopItems1.getState());
                 ps.setInt(10, shopItems1.getCategory());
                 ps.setInt(11, shopItems1.getMinLevel());
                 ps.execute();
/* 180 */               } catch (SQLException e) {

                 e.printStackTrace();
               }
             });
/* 185 */       } catch (SQLException e) {
/* 186 */         log.error("控制台插入商店数据错误", e);
       }
     }
   }


   public void loadShopData() {
/* 193 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 194 */         PreparedStatement ps = con.prepareStatement("SELECT * FROM shops"); 
/* 195 */         ResultSet rs = ps.executeQuery()) {
/* 196 */       while (rs.next()) {
/* 197 */         this.shopIDs.put(Integer.valueOf(rs.getInt("shopid")), Integer.valueOf(rs.getInt("npcid")));

       }
     }
/* 201 */     catch (SQLException e) {
/* 202 */       log.error("加载NPC商店错误", e);
     }

/* 205 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 206 */     for (Map.Entry<Integer, Integer> entry : this.shopIDs.entrySet()) {

/* 208 */       MapleShop shop = new MapleShop(((Integer)entry.getKey()).intValue(), ((Integer)entry.getValue()).intValue());
/* 209 */       try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 210 */           PreparedStatement ps = con.prepareStatement("SELECT * FROM shopitems WHERE shopid = ? ORDER BY position ASC")) {
/* 211 */         ps.setInt(1, ((Integer)entry.getKey()).intValue());
/* 212 */         try (ResultSet rs = ps.executeQuery()) {
/* 213 */           while (rs.next())
           {
/* 215 */             rs.previous();
/* 216 */             List<Integer> recharges = new ArrayList<>(rechargeableItems);
/* 217 */             int position = 1;
/* 218 */             while (rs.next()) {
/* 219 */               if (!ii.itemExists(rs.getInt("itemid")) || blockedItems.contains(Integer.valueOf(rs.getInt("itemid")))) {
                 continue;
               }
/* 222 */               if (ItemConstants.is飞镖道具(rs.getInt("itemid")) || ItemConstants.is子弹道具(rs.getInt("itemid"))) {
/* 223 */                 MapleShopItem starItem = new MapleShopItem((short)1, rs.getInt("itemid"), rs.getInt("price"), rs.getInt("reqitem"), rs.getInt("reqitemq"), rs.getInt("pointtype"), rs.getInt("period"), rs.getInt("state"), rs.getInt("category"), rs.getInt("minLevel"), position, true);
/* 224 */                 shop.addItem(starItem);
/* 225 */                 if (rechargeableItems.contains(Integer.valueOf(starItem.getItemId()))) {
/* 226 */                   recharges.remove(Integer.valueOf(starItem.getItemId()));
                 }
               } else {
/* 229 */                 shop.addItem(new MapleShopItem((short)1000, rs.getInt("itemid"), rs.getInt("price"), rs.getInt("reqitem"), rs.getInt("reqitemq"), rs.getInt("pointtype"), rs.getInt("period"), rs.getInt("state"), rs.getInt("category"), rs.getInt("minLevel"), position, false));
               }
/* 231 */               position++;
             }

/* 234 */             for (Integer recharge : recharges) {
/* 235 */               shop.addItem(new MapleShopItem((short)1, recharge.intValue(), 0, 0, 0, 0, 0, 0, 0, 0, 0, true));
             }
           }

         }
/* 240 */       } catch (SQLException e) {
/* 241 */         log.error("添加商店数据错误", e);
       }
/* 243 */       this.shops.put(entry.getKey(), shop);
     }
/* 245 */     log.trace("商店数据加载完成.");
   }

   public void clear() {
/* 249 */     this.shopIDs.clear();
/* 250 */     this.shops.clear();
/* 251 */     loadShopData();
   }

   public MapleShop getShop(int shopId) {
/* 255 */     return loadShop(shopId);
   }

   public MapleShop getShopForNPC(int npcId) {
/* 259 */     if (!this.shopIDs.values().contains(Integer.valueOf(npcId))) {
/* 260 */       return null;
     }
/* 262 */     int shopId = 0;
/* 263 */     for (Map.Entry<Integer, Integer> entry : this.shopIDs.entrySet()) {
/* 264 */       if (npcId == ((Integer)entry.getValue()).intValue()) {
/* 265 */         shopId = ((Integer)entry.getKey()).intValue();
       }
     }
/* 268 */     return loadShop(shopId);
   }

   private MapleShop loadShop(int id) {
/* 272 */     return this.shops.get(Integer.valueOf(id));
   }

   public Map<Integer, MapleShop> getAllShop() {
/* 276 */     return this.shops;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\shop\MapleShopFactory.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */